### Create coefficient plots ##
  
## 1. Training (Treatment Effects) #
  
  # open data and select dependent variables #
  
  agro = read_dta("output/data.dta") %>% group_by(id) %>% 
    mutate(lag_extension = lag(extension), lag_sprec = lag(sprec), 
           lag_rotation = lag(rotation), lag_manage = lag(manage),
           lag_conserv = lag(conserv), lag_machine = lag(machine),
           lag_pest = lag(pest), lag_l_rev = lag(l_rev), lag_l_exp = lag(l_exp)) %>%
    arrange(id,time) %>% ungroup()
  
  lagvars = rev(c("lag_extension","lag_sprec","lag_rotation","lag_manage","lag_conserv",
                  "lag_machine","lag_pest","lag_l_exp"))
  
  depvars = rev(c("extension","sprec","rotation","manage","conserv","machine","pest","l_exp"))
  
  agro = subset(agro, CAP_ATEG==0 & time==1)
  
  treatment = c("CAP")
  
  # obtain coefficients + build figure #  
  
  source("codes/R/build_effects.R", encoding = "UTF-8")
  
  # title and export #   
  
  graph.cap1 = graph + ggtitle("Panel B: Treatment Effects")
  
  ggsave(graph.cap1, file = "figures/figure2B.png", width=10, height=7.5, dpi = 600)  
  
## 2. Training (Balance) ## 
  
  # open data and select dependent variables #  
  
  agro = read_dta("output/data.dta")
  
  agro = subset(agro, CAP_ATEG==0 & time==0)
  
  depvars = rev(c("extension","sprec","rotation","manage","conserv","machine","pest","l_exp"))
  
  treatment = c("CAP")
  
  # obtain coefficients + build figure #  
  
  source("codes/R/build_balance.R", encoding = "UTF-8")
  
  # title and export #  
  
  graph.cap0 = graph + ggtitle("Panel A: Balance")
  
  ggsave(graph.cap0, file = "figures/figure2A.png", width=10, height=7.5, dpi = 600)  
  
## 3. Training + Technical Assistance (Treatment Effects) #
    
  # open data and select dependent variables #
  
      agro = read_dta("output/data.dta") %>% group_by(id) %>% 
              mutate(lag_extension = lag(extension), lag_sprec = lag(sprec), 
                     lag_rotation = lag(rotation), lag_manage = lag(manage),
                     lag_conserv = lag(conserv), lag_machine = lag(machine),
                     lag_pest = lag(pest), lag_l_rev = lag(l_rev), lag_l_exp = lag(l_exp)) %>%
        arrange(id,time) %>% ungroup()
      
      lagvars = rev(c("lag_extension","lag_sprec","lag_rotation","lag_manage","lag_conserv",
                  "lag_machine","lag_pest","lag_l_exp"))
      
      depvars = rev(c("extension","sprec","rotation","manage","conserv","machine","pest","l_exp"))
    
      agro = subset(agro, CTRL==0 & time==1)

      treatment = c("CAP_ATEG")
    
  # obtain coefficients + build figure #  
      
      source("codes/R/build_effects.R", encoding = "UTF-8")
      
  # title and export #  
      
    graph.ateg1 = graph + ggtitle("Panel B: Treatment Effects")
    
    ggsave(graph.ateg1, file = "figures/figure3B.png", width=10, height=7.5, dpi = 600)  
  
## 4. Training + Technical Assistance (Balance) ## 

  # open data and select dependent variables #  
      
      agro = read_dta("output/data.dta")
  
      agro = subset(agro, CTRL==0 & time==0)
  
      depvars = rev(c("extension","sprec","rotation","manage","conserv","machine","pest","l_exp"))
  
      treatment = c("CAP_ATEG")
      
  # obtain coefficients + build figure #  
      
      source("codes/R/build_balance.R", encoding = "UTF-8")
  
  # title and export #  
      
    graph.ateg0 = graph + ggtitle("Panel A: Balance")
  
    ggsave(graph.ateg0, file = "figures/figure3A.png", width=10, height=7.5, dpi = 600)